<?php 
header ( "Content-type: text/html; charset=utf-8" );
include "wechat.class.php";

$options = array (
		'appid' =>'wx2506434984b44fa7',
		'appsecret'=>'093dc1f822d6cb241a00f6455cf8f987',
		'token' => 'mytokentest', // 填写你设定的key
		'encodingaeskey' => '55cf8f987' 
)
// 填写加密用的EncodingAESKey，如接口为明文模式可忽略
;
$weObj = new Wechat ( $options );

$redirectUri = $weObj->getOauthRedirect('http://junjun.co/weixin-junjun/activity.php','oauth');

if($_COOKIE['access_token']&&$_COOKIE['openid']&&$_COOKIE['refresh_token']){
	//已经有token
	//尝试获取用户信息
	$info = $weObj->getOauthUserinfo($_COOKIE['access_token'], $_COOKIE['openid']);
	if(!$info){
		$token = $weObj->getOauthRefreshToken($_COOKIE['refresh_token']);
		if($token){
			//授权了
			echo '刷新授权结果';
			var_dump($token);
			foreach ($token as $k=>$v){
				$_COOKIE[$k] = $v;
			}
			
			$info = $weObj->getOauthUserinfo($token['access_token'], $token['openid']);
			var_dump($info);
			
		}else{
			//没授权
			echo '刷新token失败，<a href="'.$redirectUri.'">再来一次</a>';
		}
	}else{
		var_dump($info);
	}
}elseif($_GET['state'] === 'oauth'){
	//已经授权结果
	$token = $weObj->getOauthAccessToken();
	if($token){
		//授权了
		echo '授权结果';
		var_dump($token);
		foreach ($token as $k=>$v){
			$_COOKIE[$k] = $v;
		}
		$info = $weObj->getOauthUserinfo($token['access_token'], $token['openid']);
		var_dump($info);
	}else{
		//没授权
		echo '木有授权，<a href="'.$redirectUri.'">再来一次</a>';
	}
}else{
	//啥都没有
	header( 'Location: '.$redirectUri);
}

var_dump($_REQUEST);